<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>表单构建</title>
    <div th:replace="common/link::header"></div>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            自定义表单
        </div>
        <div class="layui-card-body">
            <div class="layui-row">
                <div class="layui-col-md4">
                    <div class="layui-form layui-form-pane" lay-filter="formDataForm" id="formDataForm">
                        <div class="layui-form-item">
                            <label class="layui-form-label">表单名称</label>
                            <div class="layui-input-block">
                                <input type="text" class="layui-input" name="formName" placeholder="请输入表单名称"/>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">表单KEY</label>
                            <div class="layui-input-block">
                                <input type="text" name="formKey" lay-verify="required" placeholder="请输入表单KEY" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-block">
                                <textarea class="layui-textarea" name="remark" placeholder="请输入备注"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">创建者</label>
                            <div class="layui-input-block">
                                <input type="text" class="layui-input layui-disabled" name="createBy" disabled="disabled"/>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">创建时间</label>
                            <div class="layui-input-block">
                                <input type="text" class="layui-input layui-disabled" name="createTime" disabled="disabled"/>
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <input type="button" lay-submit lay-filter="save-submit" id="save-submit" value="确认">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md8">
                    <div class="build-wrap"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:replace="common/script::footer"></div>
<script th:src="@{/static/lib/form-builder/form-builder.min.js}"></script>
<script th:src="@{/static/lib/form-builder/form-render.min.js}"></script>
<script th:inline="javascript">
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index', //主入口模块
    }).use(['index', 'form'], function(){
        let $ = layui.$,
            form = layui.form;
        // 初始化formBuilder
        let formBuilder = $('.build-wrap').formBuilder();
        // 保存后提交
        form.on('submit(save-submit)', function(data) {
            data.field.formXmlData = formBuilder.actions.getData('xml');
            data.field.formJsonData = formBuilder.actions.getData('json');
            $.ajax({
                type: 'POST',
                url:  ctx + '/act/formData/add',
                data: JSON.stringify(data.field),
                contentType:'application/json;charset=UTF-8',
                dataType: 'json',
                success: function(result) {
                    layer.msg(result.message);
                    if (result.code === 200) {
                        let index = parent.layer.getFrameIndex(window.name);
                        // 关闭
                        parent.layer.close(index);
                        // 刷新
                        parent.location.reload();
                    }
                }
            });
        });
    });
</script>
</body>
</html>